package com.example.redstarcommerce.cn.dh.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.redstarcommerce.cn.dh.common.vo.Result;
import com.example.redstarcommerce.cn.dh.coupon.mapper.CouPonMapper;
import com.example.redstarcommerce.cn.dh.coupon.pojo.CouPon;
import com.example.redstarcommerce.cn.dh.coupon.service.CouPonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @ClassName CouPonServiceImpl
 * @Author AndyWu
 * @Date 2024-11-01 22:44
 * @Motto 学不死就往死里学
 * @Version 1.0
 */
@Service
@Slf4j
public class CouPonServiceImpl implements CouPonService {

    @Autowired
    private CouPonMapper couPonMapper;

    @Override
    public Result<Boolean> insertCouPon(String name, String type, Float price, Integer num, Timestamp begintime, Timestamp endtime) {
        CouPon couPon = new CouPon(name,type,price,num,begintime,endtime);
        int insert = couPonMapper.insert(couPon);
        if (insert <= 0){
            return Result.fail(false);
        }
        return Result.success(true);
    }


    @Override
  public Result<List<CouPon>> getByCouPon(String name, String type, Float price, Integer num, Timestamp begintime, Timestamp endtime) {
      // 创建查询条件FXXK
      QueryWrapper<CouPon> queryWrapper = new QueryWrapper<>();
      // 查询未过期的优惠卷
      queryWrapper.gt("endtime", new Timestamp(System.currentTimeMillis()));
      // 执行查询
      List<CouPon> list = couPonMapper.selectList(queryWrapper);

      // 打印查询时间点和查询到的优惠券列表
      Timestamp timestamp = new Timestamp(System.currentTimeMillis());
      System.out.println("coupons at " + timestamp + ": " + list);

      // 判断查询的优惠卷是否为空
      if (list.isEmpty()) {
          return Result.fail("无可用优惠卷");
      } else {
          return Result.success(list);
      }
  }



@Override
public Result<Boolean> updateCouPon(Integer id, String name, String type, Float price, Integer num, Timestamp begintime, Timestamp endtime) {
    // 检查ID是否为空
    if (id == null) {
        return Result.fail("用户为空");
    }
    // 创建查询条件
    QueryWrapper<CouPon> queryWrapper = new QueryWrapper<>();
    QueryWrapper<CouPon> idUser = queryWrapper.eq("id", id);
    // 根据ID查询优惠券
    CouPon couPon = couPonMapper.selectOne(idUser);
    // 如果优惠券存在，则更新其信息
    if (couPon != null) {
        couPon.setName(name);
        couPon.setType(type);
        couPon.setPrice(price);
        couPon.setNum(num);
        couPon.setBegintime(begintime);
        couPon.setEndtime(endtime);
        // 设置当前时间作为结束时间
        couPon.setEndtime(new Timestamp(System.currentTimeMillis()));

        // 执行更新操作
        boolean b = couPonMapper.update(couPon, idUser) > 0;
        // 返回更新结果
        return Result.success(b);
    }
    // 如果优惠券不存在，返回null
    return null;
}
@Override
public Result<Boolean> deleteCouPon(Integer id) {
    // 创建一个查询条件包装器，用于构建删除操作的条件
    QueryWrapper<CouPon> queryWrapper = new QueryWrapper<>();
    // 设置查询条件，根据优惠券ID进行等值查询
    queryWrapper.eq("id",id);
    // 执行删除操作，根据查询条件删除优惠券
    couPonMapper.delete(queryWrapper);
    // 返回删除成功的结果
    return Result.success(true);
}

    @Override
    public Result<List<CouPon>> getAllCouPon() {
        List<CouPon> list = couPonMapper.selectList(null);
        return Result.success(list);

    }


}












